package com.csoa.community.db;

import android.database.Cursor;

import com.csoa.community.bean.LoginBean;

public class LoginDB extends DataBaseDB {

	/**
	 * 登陆成功保存数据到本地
	 * 
	 * @param lm
	 */
	public static void insert(LoginBean loginBean) {
		DataBaseDB.getInst().getDB();
		String sql = "insert into loginInfoTable(id,userName,password,autoLogin,currentLogin,loginTime,nickName) values(?,?,?,?,?,?,?)";
		db.beginTransaction();
		try {
			db.execSQL(sql, new String[] { loginBean.id, loginBean.userName,
					loginBean.autoLogin, loginBean.currentLogin,
					loginBean.loginTime, loginBean.nickName });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
	}

	/**
	 * 查询某个用户的登录信息
	 * @param lm
	 */
	public static LoginBean query(String id) {
		DataBaseDB.getInst().getDB();
		String sql = "select id,userName,password,autoLogin,currentLogin,loginTime,nickName from loginInfoTable where id=?";
		db.beginTransaction();
		Cursor cursor = null;
		LoginBean loginBean = null;
		try {
			cursor = db.rawQuery(sql, new String[] { id });
			if (cursor.getCount() > 0) {
				cursor.moveToNext();
				loginBean = new LoginBean();
				loginBean.id = cursor.getString(cursor.getColumnIndex("id"));
				loginBean.userName = cursor.getString(cursor.getColumnIndex("userName"));
				loginBean.password = cursor.getString(cursor.getColumnIndex("password"));
				loginBean.autoLogin = cursor.getString(cursor.getColumnIndex("autoLogin"));
				loginBean.currentLogin = cursor.getString(cursor.getColumnIndex("currentLogin"));
				loginBean.loginTime = cursor.getString(cursor.getColumnIndex("loginTime"));
				loginBean.nickName = cursor.getString(cursor.getColumnIndex("nickName"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;

			}
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
		return loginBean;
	}




	/**
	 * 删除某一个用户的登录信息
	 * 
	 * @param LoginBean
	 */
	public static void delete(LoginBean loginBean) {
		DataBaseDB.getInst().getDB();
		String del = "delete from loginInfoTable where id=?";
		db.beginTransaction();
		try {
			db.execSQL(del, new String[] { loginBean.id });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}

	}

	/**
	 * 更新某个用户的登录信息
	 * 
	 * @param lm
	 */
	public static boolean update(LoginBean loginBean) {
		DataBaseDB.getInst().getDB();
		String sql = "update loginInfoTable set userName=?,password=?,autoLogin=?,currentLogin=?,loginTime=?,nickName=? where id=?";
		db.beginTransaction();
		try {
			db.execSQL(
					sql,
					new String[] {loginBean.userName,loginBean.userName,
							loginBean.autoLogin, loginBean.currentLogin,
							loginBean.loginTime, loginBean.nickName,loginBean.id});
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (db.isOpen()) {
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
		}
		return true;
	}
}
